package net.wanho.config;

import net.wanho.constant.SystemConstant;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
public class ResourceConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(@NotNull ResourceHandlerRegistry registry) {
        String fileUrl = SystemConstant.STATIC_RESOURCE_PATH;
        
        // 检查目录是否存在
        File uploadDir = new File(fileUrl);
        if (!uploadDir.exists()) {
            boolean created = uploadDir.mkdirs();
            if (!created) {
                throw new RuntimeException("上传文件夹创建失败！");
            }
        }

        // 配置资源处理器
        registry.addResourceHandler("/statics/**")
                .addResourceLocations("file:" + fileUrl)
                .setCachePeriod(3600)
                .resourceChain(true);

    }
}
